package psd.model;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Date;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Scanner;

import psd.config.ConfigurationReader;

public class StadisticManager {
	
	
	StringBuilder contents = null;
	private static StadisticManager instance = null;
	
	private StadisticManager(){
		contents = new StringBuilder();
	}
	
	public static StadisticManager getInstance()
	{
		if (instance == null)
		{
			instance = new StadisticManager();
		}
		return instance;
	}
	
	public String StoreStadisticsFile() {
		try {	
			FileWriter writer = new FileWriter(this.getStadisticsFilePath(), true);
			BufferedWriter bw = new BufferedWriter(writer);
			PrintWriter stadisticWriter = new PrintWriter(bw);
			Scanner sc = new Scanner(contents.toString());
			
			while (sc.hasNextLine()) {
				stadisticWriter.println(sc.nextLine());
			}
			stadisticWriter.flush();
			sc.close();
			stadisticWriter.close();
		
			File readFile = new File(this.getStadisticsFilePath());
			StringBuilder text = new StringBuilder();
			String NL = System.getProperty("line.separator");
			Scanner scanner = new Scanner(readFile);
			while (scanner.hasNextLine())
			   text.append(scanner.nextLine() + NL);
			 scanner.close();
			 return text.toString();
			 
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return "";		
		
	}
	
	public void deleteStadisticsFile()
	{
		try{
			File file = new File(this.getStadisticsFilePath());
			boolean del = file.delete();
			String a = "asd";
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
	}
	
	public void writeStadistic(Bug bug)
    {
		DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd");
		Date date = new Date(System.currentTimeMillis());
		
		contents.append(System.getProperty("user.name"));
		contents.append("\t");
		contents.append(bug.get_path());
		contents.append("\t");
		contents.append(bug.get_lineNumber());
		contents.append("\t");
		contents.append(bug.get_description());
		contents.append("\t");
		contents.append(bug.get_ruleid());
		contents.append("\t");
		contents.append(bug.get_ruleName());
		contents.append("\t");
		contents.append(dateFormat.format(date).toString());
		contents.append(System.getProperty("line.separator"));
    }
	
	public String getStadistics()
	{
		String returnString =this.StoreStadisticsFile();
		instance = null;
		return returnString;
	} 
	
	private String getStadisticsFilePath()
	{
		String stadisticsFilePath = ConfigurationReader.Instance().getProperty("stadisticsPath");
		if(!stadisticsFilePath.isEmpty())
		{			
			File f = new File(ConfigurationReader.Instance().getProperty("stadisticsPath"));
			if(!f.exists())
				f.mkdirs();
			stadisticsFilePath += "//stadistics.txt";
		}else
			stadisticsFilePath = "stadistics.txt";
		//agregar nombre de usuario.
		return stadisticsFilePath;
	}
}
